<?php

defined('EN_CODE') or die(__FILE__.': Direct Access Denied');

use Code\Model;


function formas_pago() {
    $model = Model::getInstance();
    $query = "SELECT * FROM formas_pago";
    $result = $model->query($query);
    if (!$result || $model->numRows($result)==0) {
        return null;
    }
    return $model->fetchAll($result);
}

function listado_total_ventas() {
    $model = Model::getInstance();
    $query = "SELECT * FROM ventas";
    $result = $model->query($query);
    if (!$result || $model->numRows($result)==0) {
        return null;
    }
    return $model->fetchAll($result);
}

function una_venta($numero_venta) {
    $model = Model::getInstance();
    $numero_venta_escaped = preg_replace('/[^\d]/', '', $numero_venta);
    $query = "SELECT v.*, c.nombre AS cliente, fp.nombre AS pago
              FROM ventas v
                INNER JOIN clientes c USING(numero_cliente)
                INNER JOIN formas_pago fp USING(forma_pago)
              WHERE numero_venta=$numero_venta_escaped";
    $result = $model->query($query);
    return $model->fetchOne($result);
}

?>
